SUBROUTINE vfunapprox_theta_ability_steal_fine(emaxin,betain)
  USE Commonvars
  IMPLICIT NONE
  INCLUDE 'mpif.h'
  
  REAL(8), INTENT(IN)  :: emaxin(Ntheta,Nab,Nwealth,Nsteal,Nfines)
  REAL(8), INTENT(OUT) :: betain(MMnew)
  REAL(8), ALLOCATABLE :: y(:), xvar(:,:), res(:,:,:,:,:), vfunout(:,:,:,:,:), ones(:), betastart(:), xpx(:,:), xpy(:), invxx(:,:), res_final(:), work(:)
  REAL(8)              :: Rsquared, av_res(Nwealth), av_res_temp(Nwealth), v_slope, v_slope_p
  INTEGER              :: ii, i, j, iwealth, ia, itheta, info, MM_t, lwork, isteal, ifine
  CHARACTER :: uplo='U'

  ii = Ntheta*Nab*Nwealth*Nsteal*Nfines

  ALLOCATE(y(ii), res(Ntheta,Nab,Nwealth,Nsteal,Nfines), ones(ii), vfunout(Ntheta,Nab,Nwealth,Nsteal,Nfines), res_final(ii))
  y = 0d0; res = 0d0

  MM_t = Npol_ab + Npol_theta + Npol_st + Npol_fine
  ALLOCATE(xvar(ii,MM_t),xpx(MM_t,MM_t), xpy(MM_t), invxx(MM_t,MM_t), betastart(MM_t))

  xpx = 0d0; xpy = 0d0
  betastart = 0d0
  
  !Average the residuals for each iwealth
  av_res_temp = 0d0
  DO iwealth = 1, Nwealth
     DO ia = 1, Nab
        DO itheta = 1, Ntheta
           DO isteal = 1, Nsteal
              DO ifine = 1, Nfines
                 av_res_temp(iwealth) = av_res_temp(iwealth) + emaxin(itheta,ia,iwealth,isteal,ifine)
              END DO
           END DO
        END DO
     END DO
  END DO
  av_res_temp = av_res_temp/DBLE(Ntheta*Nab*Nsteal*Nfines)

!!$  IF (flag_here == 2858) THEN
!!$     WRITE(*,'(a,10f15.7)') 'av_res_temp',  av_res_temp(:)
!!$     DO iq = 1, Nq
!!$        WRITE(*,'(a,1i4,10f15.7)') 'emaxin', iq, emaxin(iq,:)
!!$     END DO
!!$  END IF

  !Compute the residuals V - (alpha_1+alpha_2*q+alpha_3*q^2)
  DO iwealth = 1, Nwealth
     DO ia = 1, Nab
        DO itheta = 1, Ntheta
           DO isteal = 1, Nsteal
              DO ifine = 1, Nfines
                 res(itheta,ia,iwealth,isteal,ifine) = emaxin(itheta,ia,iwealth,isteal,ifine) - av_res_temp(iwealth)
              END DO
           END DO
        END DO
     END DO
  END DO

  !We compute the coefficients on public consumption in V = f(sav)+alpha_1+alpha_2*q+alpha_3*q^2
  ones = 1d0
  i = 0
  DO iwealth = 1, Nwealth
     DO ia = 1, Nab
        DO itheta = 1, Ntheta
           DO isteal = 1, Nsteal
              DO ifine = 1, Nfines
              
                 i = i + 1
                 y(i) = res(itheta,ia,iwealth,isteal,ifine)
                 xvar(i,1:MM_t) = (/ ones(i), a_discr(ia), a_discr(ia)**2d0, theta(itheta), stealing(isteal), frac_fine(ifine) /)
                 
              END DO
           END DO
        END DO
     END DO
  END DO
  
  xpx = MATMUL(TRANSPOSE(xvar(:,:)),xvar(:,:))/SIZE(y)
  xpy = MATMUL(TRANSPOSE(xvar(:,:)),y(:))/SIZE(y)
     
  invxx = 0d0
  DO j=1, MM_t
     invxx(j,j)=1d0
  END DO

  LWORK = MM_t
  IF (ALLOCATED(WORK)) DEALLOCATE(WORK)
  ALLOCATE(WORK(LWORK))  
  CALL DPOSV(uplo,MM_t,MM_t,xpx,MM_t,invxx,MM_t,info)
  IF (info /= 0) THEN
     DO j = 1, ii
        WRITE(*,'(a,1i4,7f12.6)') 'y,x',j,y(j),xvar(j,1:MM_t)
     ENDDO
     WRITE(*,*) 'MATRIX COULD NOT INVERT vfun_theta_ability_steal_fine', 'info', info
  END IF
  
  betastart(:) = MATMUL(invxx,xpy)

  IF (flag_Tend == 1) THEN
     v_slope_p = (av_res_temp(2)-av_res_temp(1))/(wealth(2) - wealth(1))
     av_res(Nwealth) = av_res_temp(Nwealth)
     DO iwealth = Nwealth-1, 1, -1
        IF (av_res_temp(iwealth+1) - av_res_temp(iwealth) > small_no) THEN
           av_res(iwealth) = av_res_temp(iwealth)
           V_slope = (av_res_temp(iwealth+1)-av_res_temp(iwealth))/(wealth(iwealth+1) - wealth(iwealth))
        ELSE        
           V_slope = inc_slope*V_slope_p+V_slope_p
           av_res(iwealth) = V_slope*(wealth(iwealth) - wealth(iwealth+1)) + av_res(iwealth+1)
        END IF
        V_slope_p = V_slope
     END DO
  ELSE
     av_res = av_res_temp
  END IF
  betain(:) = (/ av_res(:), betastart(:) /)

  !Compare the actual value function with the approximation
  i = 0
  DO iwealth = 1, Nwealth
     DO ia = 1, Nab
        DO itheta = 1, Ntheta
           DO isteal = 1, Nsteal
              DO ifine = 1, Nfines
           
                 i = i + 1
                 vfunout(itheta,ia,iwealth,isteal,ifine) = av_res(iwealth) + betastart(1) + betastart(2)*a_discr(ia) + betastart(3)*a_discr(ia)**2d0 + betastart(4)*theta(itheta) + betastart(5)*stealing(isteal) + betastart(6)*frac_fine(ifine)
                 res_final(i) = emaxin(itheta,ia,iwealth,isteal,ifine) - vfunout(itheta,ia,iwealth,isteal,ifine)
                 y(i) = emaxin(itheta,ia,iwealth,isteal,ifine)

              END DO
           END DO
        END DO
     END DO
  END DO

  Rsquared = (SUM((y-SUM(y)/SIZE(y))**2d0) - SUM(res_final**2d0)) / SUM((y-SUM(y)/SIZE(y))**2d0)

  IF (Rsquared < 0.75d0 .AND. flag_Tend == 0) THEN
!!$  IF (Rsquared < 0.75d0) THEN
!!$  IF (flag_here == 1) THEN
!!$  IF (flag_Tend == 1) THEN
     IF (myrank == 0) THEN
        write(*,*) 'Rsquared vfunapprox_Tend', Rsquared
        DO iwealth = 1, Nwealth
           DO ia = 1, Nab
              DO itheta = 1, Ntheta
                 DO isteal = 1, Nsteal
                    DO ifine = 1, Nfines
                
                       write(*,'(a,5i4,5f20.10)') 'vfunapprox_theta_ability', itheta,ia,iwealth,isteal,ifine,emaxin(itheta,ia,iwealth,isteal,ifine), vfunout(itheta,ia,iwealth,isteal,ifine), av_res(iwealth) + betastart(1) + betastart(2)*a_discr(ia) + betastart(3)*a_discr(ia)**2d0 + betastart(4)*theta(itheta) + betastart(5)*stealing(isteal) + betastart(6)*frac_fine(ifine), av_res_temp(iwealth), betastart(1) + betastart(2)*a_discr(ia) + betastart(3)*a_discr(ia)**2d0 + betastart(4)*theta(itheta) + betastart(5)*stealing(isteal) + betastart(6)*frac_fine(ifine)
                       
                    END DO
                 END DO                 
              END DO
           END DO
        END DO
        write(*,*) 'betas', betain(:)
     END IF
     flag_here = 146
  END IF
  
  DEALLOCATE(y,xvar,res,vfunout)

END SUBROUTINE vfunapprox_theta_ability_steal_fine

